package com.bdqn.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdqn.crm.pojo.BasDict;
import com.bdqn.crm.pojo.CstCustomer;
import com.bdqn.crm.pojo.SalChance;
import com.bdqn.crm.service.BasDictService;
import com.bdqn.crm.service.CstCustomerService;
import com.bdqn.crm.utils.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/customer")
public class CstCustomerController {

    @Resource
    private CstCustomerService cstCustomerService;
    @Resource
    private BasDictService basDictService;

    @RequestMapping("/list")
    public String list(HttpServletRequest request, Model model, @RequestParam(value = "custName",required = false) String custName, @RequestParam(value = "custRegion",required = false) String custRegion, @RequestParam(required = false, defaultValue = "1") int pageIndex){
        Page<CstCustomer> page = new Page<>(pageIndex, 5);
        QueryWrapper<CstCustomer> queryWrapper = new QueryWrapper<>();
        //判断是否有查询条件
        if (custName != null && custName.trim() != "") {
            queryWrapper.like("cust_name", custName);      //模糊设置条件
        }
        if (custRegion != null && custRegion.trim() != "") {
            queryWrapper.like("cust_region", custRegion);
        }
        if (cstCustomerService.count(queryWrapper) == 0) {     //此操作说明 数据库没有数据可展示，那么就全部清零
            page.setTotalElements(0);
            page.setContent(null);
        } else {
            page.setTotalElements(cstCustomerService.count(queryWrapper));
            page.setContent(cstCustomerService.selectPages(custName, custRegion, (page.getNumber() - 1) * page.getPageSize(), page.getPageSize()));
        }

        //地区
        QueryWrapper<BasDict> basDictQueryWrapper = new QueryWrapper<>();
        basDictQueryWrapper.eq("dict_type","地区");
        List<BasDict> basDictList =basDictService.list(basDictQueryWrapper);
        model.addAttribute("dictList",basDictList);
        model.addAttribute("custName",custName);
        model.addAttribute("custRegion",custRegion);
        model.addAttribute("page", page);
        return "/customer/list";
    }
}
